import { isDev } from '../config/index'
import router from '@/router'
import axios from 'axios'
import { Toast } from 'mand-mobile'

import { AxiosRequestConfig } from 'axios'

export const instance = axios.create({
    baseURL: isDev ? '/api' : '/qcApi',
    timeout: 15000
})

instance.interceptors.request.use(cfg => {
    // cfg.headers.Token = getStorage('Token') || ''
    return cfg
})

instance.interceptors.response.use(
    res => {
        if (res.data.retCode === '202') {
            router.push('/')
            return
        }
        return res as any
    },
    err => {
        err.response
            ? Toast.failed('错误码：' + err.response.status)
            : Toast.failed('请求超时...')
    }
)

interface Response<T = any> {
    retMessage: string
    retData?: T
    data?: T
    retCode: number
}

export const Api = <T = any>(config: AxiosRequestConfig) => {
    return instance(config).then(({ data }) => data as Response<T>)
}
